import java.util.HashMap;
import java.util.Map;

/**
 * Created by 86135 on 2020/2/19.
 */
public class Test09 {
    public static void main(String[] args) {

        String s1 = "{'name':'张同学'}";
        String s2 = "{'name':'张同学','age':20}";
        String s3 = "{'name':'张同学','age':20,'birthday':'1999-10-06'}";

        System.out.println("------- 字符串S1解析结果如下 -------");
        Map<String, String> map1 = parseProperties(s1);
        for(String key:map1.keySet()){
            System.out.println("key = "+key+" ,value = "+map1.get(key));
        }

        System.out.println("------- 字符串S2解析结果如下 -------");
        Map<String, String> map2 = parseProperties(s2);
        for(String key:map2.keySet()){
            System.out.println("key = "+key+" ,value = "+map2.get(key));
        }

        System.out.println("------- 字符串S3解析结果如下 -------");
        Map<String, String> map3 = parseProperties(s3);
        for(String key:map3.keySet()){
            System.out.println("key = "+key+" ,value = "+map3.get(key));
        }

    }

    /**
     * @param str
     * @return
     */
    public static Map<String,String> parseProperties(String str){
        //补全代码实现字符串解析
        Map<String,String> map = new HashMap<>();
        String s = str.replace("'", "").replace("{","").replace("}","");
        String[] split = s.split(",");
        for (int i = 0; i < split.length; i++) {
            String[] s1 = split[i].split(":");
            map.put(s1[0],s1[1]);
        }

        return map;

    }
}
